/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;

import com.asseco.mio.BasicInfo;
import com.asseco.mio.WorkPosition;
import com.asseco.mio.codex.BasicInfoForm;
import com.asseco.mio.codex.Country;
import com.asseco.mio.codex.EduLvl;
import com.asseco.mio.codex.GeoArea;
import com.asseco.mio.codex.Municipality;
import com.asseco.mio.codex.Nationality;
import com.asseco.persistence.service.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.service.GenericManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author filip.apostolski
 */
@RequestMapping("/basicinfo*")
@Controller

public class BasicInfoController {

    private GenericManager<BasicInfo, String> basicInfoManager;
    private GenericManager<Municipality, String> municipalityManager = null;
    private GenericManager<Nationality, String> nationalityManager = null;
    private GenericManager<EduLvl, String>eduLvlManager = null;




    @Autowired
    public void setMunicipalityManager(@Qualifier("municipalityManager") GenericManager<Municipality, String> municipalityManager) {
        this.municipalityManager = municipalityManager;
    }

    @Autowired
    public void setNationalityManager(@Qualifier("nationalityManager") GenericManager<Nationality, String> nationalityManager) {
        this.nationalityManager = nationalityManager;
    }

    @Autowired
    public void setBasicInfoManager(@Qualifier("basicInfoManager") GenericManager<BasicInfo, String> basicInfoManager) {
        this.basicInfoManager = basicInfoManager;
    }

    @Autowired
    public void setEduLvlManager(@Qualifier("eduLvlManager") GenericManager<EduLvl, String> eduLvlManager) {
        this.eduLvlManager = eduLvlManager;
    }


    public List getBasicInfo(BasicInfo basicInfo) {
        if (basicInfo==null){
            return null;
        }
        if (basicInfo.getFirstName().equals("")&&basicInfo.getLastName().equals("")&&basicInfo.getSocScrNo().equals("")&&basicInfo.getGender().equals("")&&basicInfo.getNationality().getId().equals("")&&basicInfo.getMunicipality().getId().equals("")&&basicInfo.getEduLvl().getId().equals("")){
            return null;
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String pamFirstName = "%";
        String pamLastName = "%";
        String pamSocScrNo = "%";
        String pamNationality = "%";
        String pamMunicipality = "%";
        String pamGender = "%";
        String pamEduLvl = "%";

        if (basicInfo.getFirstName()!=null)
            if (!basicInfo.getFirstName().equals(""))
            pamFirstName = "%" + basicInfo.getFirstName() + "%";
        if (basicInfo.getLastName()!=null)
            if(!basicInfo.getLastName().equals(""))
            pamLastName = "%" + basicInfo.getLastName() + "%";
        if (basicInfo.getSocScrNo()!=null )
            if (!basicInfo.getSocScrNo().equals(""))
            pamSocScrNo = "%" + basicInfo.getSocScrNo() + "%";
        if (basicInfo.getNationality()!=null) {
         if(!basicInfo.getNationality().getId().equals("")) {
                Nationality x = nationalityManager.get(basicInfo.getNationality().getId());
                basicInfo.setNationality(x);
                pamNationality = "%" + basicInfo.getNationality().getNationalityName() + "%";
            }
        }
        if (basicInfo.getMunicipality()!=null) {
            if(!basicInfo.getMunicipality().getId().equals("")) {
                basicInfo.setMunicipality(municipalityManager.get(basicInfo.getMunicipality().getId()));
                pamMunicipality = "%" + basicInfo.getMunicipality().getName() + "%";
            }
        }
        if (basicInfo.getGender()!=null)
            if(!basicInfo.getGender().equals(""))
                pamGender =  basicInfo.getGender() + "%";
        if (basicInfo.getEduLvl()!=null) {
            if(!basicInfo.getEduLvl().getId().equals("")) {
                basicInfo.setEduLvl(eduLvlManager.get(basicInfo.getEduLvl().getId()));
                pamEduLvl = "%" + basicInfo.getEduLvl().getEduLvlName() + "%";
            }
        }

        Query q = session.createQuery("from BasicInfo basicInf where ( "
                + "basicInf.firstName like '" + pamFirstName + "' and "
                + "basicInf.lastName like '" + pamLastName + "' and "
                + "basicInf.socScrNo like '" + pamSocScrNo + "' and "
                + "basicInf.nationality.nationalityName like '" + pamNationality + "' and "
                + "basicInf.municipality.name like '" + pamMunicipality + "' and "
                + "basicInf.gender like '" + pamGender + "' and "
                + "basicInf.eduLvl.eduLvlName like '" + pamEduLvl + "') ");


        List tempResult = q.list();
        System.out.println(q);
        List result = new ArrayList();
        for (int j = 0; j < tempResult.size(); j++) {
            BasicInfo object = (BasicInfo) tempResult.get(j);
            result.add(object);
        }
        session.getTransaction().commit();
        return result;

    }

    @ModelAttribute
    public BasicInfoForm basicInfoForm() {
        
         BasicInfoForm x = new BasicInfoForm(null, null,  getAllActiveMunicipality(this.municipalityManager.getAll()),getAllActiveNationality(this.nationalityManager.getAll()),getAllActiveEduLvl(this.eduLvlManager.getAll()));
         return x;
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView doSubmit(BasicInfoForm basicInfoForm, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {


            BasicInfoForm x = this.basicInfoForm();
            if(basicInfoForm.getBasicInfo()!=null){
                x.setBasicInfo(basicInfoForm.getBasicInfo());
            }
            else{
                x.setBasicInfo(null);
            }



            //////////////

            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();


            List tempResult = session.createSQLQuery("Select grad_id from BAZA.grad").list();

            System.out.println("*********************************" + tempResult.toString());
                 session.getTransaction().commit();
            



        return new ModelAndView("basicinfo", "searchList", getBasicInfo(x.getBasicInfo()));
    }
    
    
        public List getAllActiveGeoArea(List<GeoArea> list) {

        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            GeoArea ga = list.get(i);
            if (ga.getStatus() == true) {
                result.add(ga);
            }

        }

        return result;
    }

    public List getAllActiveCountry(List<Country> list) {

        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Country ga = list.get(i);
            if (ga.getStatus() == true) {
                result.add(ga);
            }

        }

        return result;
    }

    public List getAllActiveNationality(List<Nationality> list) {

        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Nationality ga = list.get(i);
            if (ga.getStatus() == true) {
                result.add(ga);
            }

        }

        return result;
    }

    public List getAllActiveMunicipality(List<Municipality> list) {

        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Municipality ga = list.get(i);
            if (ga.getStatus() == true) {
                result.add(ga);
            }

        }

        return result;
    }
     public List getAllActiveEduLvl(List<EduLvl> list) {

        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            EduLvl ga = list.get(i);
            if (ga.getStatus() == true) {
                result.add(ga);
            }

        }

        return result;
    }
}
